package module.ahleteView;

import util.UI.SwingUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ Auther  leiHua
 * @ Date  2022/5/21 14:58
 * 实现运动员界面内容类
 * 该内容包括俩部分 ：  1：按钮导航条 barJpanel  2：按钮相对应的内容
 */
public class AthleteButtonBar {
    private JButton index = new JButton("首页");
    private JButton baoming = new JButton("报名");
    private JButton myCompetetion = new JButton("我的比赛信息");
    private JButton exit = new JButton("退出系统");
    //按钮的 容器
    private JPanel barJpanel = new JPanel();
    //选中的按钮颜色
    Color buttonColor = new Color(144, 246, 186);
    //主要的内容容器
    JPanel content ;
    //包裹整个内容的Panel
    JPanel jPanel = new JPanel();
    //对应四个按钮，用来实现 点击后按钮变色  ，和当前内容板块对应的按钮变色
    boolean[] flag = new boolean[4];


    /**
     * 构造方法
     */
    public AthleteButtonBar() {
        jPanel.setLayout(null);
        SwingUI.setUI();
        barJpanel.setSize(1200, 60);
        barJpanel.setBackground(new Color(116, 125, 140));
        barJpanel.setBounds(0, 0, barJpanel.getWidth(), barJpanel.getHeight());
        Font font = new Font("宋体", Font.BOLD, 28);

        //添加监听器
        index.addActionListener(new AthleteButtonClick());
        baoming.addActionListener(new AthleteButtonClick());
        myCompetetion.addActionListener(new AthleteButtonClick());
        exit.addActionListener(new AthleteButtonClick());

        //设置按钮的字体
        index.setFont(font);
        baoming.setFont(font);
        myCompetetion.setFont(font);
        exit.setFont(font);

        barJpanel.add(index);
        barJpanel.add(baoming);
        barJpanel.add(myCompetetion);
        barJpanel.add(exit);
//        barJpanel.setBackground(new Color(46, 204, 113));

        //默认一开始的界面显示的内容是首页
        for (int i = 0; i <flag.length; i++) {
            flag[i]=false;
        }
        flag[0] = true;
        content= new AthleteIndex();
        //content.setBounds(50, 70, 1100, 1000);
        index.setBackground(buttonColor);
        jPanel.setSize(1180, 850);
        jPanel.add(barJpanel);

      //  添加主要内容的 JPenal添加内容
//        AthleteTalbe athleteTalbe = new AthleteTalbe();
//        JPanel table = athleteTalbe.creatCompetitionTable();  //全部运动项目信息表
//        System.out.println(table);
//        content = table;
//        content.setBounds(40, 70, 1100, 700);  //设置位置
//
       jPanel.add(content);
    }

    /**
     * @return 获取整一个 内容
     */
    public JPanel getjPanel() {
        return jPanel;
    }

    /**
     * 按钮条的监听器
     */
    class AthleteButtonClick implements ActionListener {


        @Override
        public void actionPerformed(ActionEvent e) {
            AthleteBaoming talbe = new AthleteBaoming();
            int i;
            String action = e.getActionCommand();

            if (action.equals(index.getText())) {     //首页按钮
                //设置按钮颜色
                for (i = 0; i <= 3; i++) {
                    flag[i] = false;
                }
                flag[0] = true;
                setButtonColor();
                jPanel.remove(content);

                content = new AthleteIndex();
                content.setBounds(50, 70, 1100, 1000);  //设置位置
                jPanel.add(content);
                jPanel.updateUI();
                jPanel.repaint();
            } else if (action.equals(baoming.getText())) {    //报名按钮
                for (i = 0; i <= 3; i++) {
                    flag[i] = false;
                }
                flag[1] = true;
                setButtonColor();
                //System.out.println("报名");

                AthleteBaoming athleteTalbe = new AthleteBaoming();
                JPanel table = athleteTalbe.creatCompetitionTable();  //全部运动项目信息表
                jPanel.remove(content);
                content = table;
                //content.setBackground(Color.red);
                content.setBounds(80, 70, 1100, 1000);  //设置位置
                jPanel.add(content);
                jPanel.updateUI();
                jPanel.repaint();
            } else if (action.equals(myCompetetion.getText())) {  // 比赛信息按钮
                for (i = 0; i <= 3; i++) {
                    flag[i] = false;
                }
                flag[2] = true;
                setButtonColor();

                JPanel table = new AthleteMyCompetition();
                //向主要内容框加入表格
                jPanel.remove(content);
                content = table;
                content.setBounds(50, 70, 1100, 1000);  //设置位置
                jPanel.add(content);
                jPanel.updateUI();
                jPanel.repaint();

            } else if (action.equals(exit.getText())) {  //退出按钮
                for (i = 0; i <= 3; i++) {
                    flag[i] = false;
                }
                flag[3] = true;
                JLabel label=new JLabel("确定退出系统吗");
                label.setFont(new Font("宋体",Font.BOLD,30));
                int a = JOptionPane.showConfirmDialog(null, label, "退出确认", JOptionPane.OK_CANCEL_OPTION);
                if (a==0){  //用户点击了确定
                    System.exit(0);
                }
                setButtonColor();
                //退出按钮
            }


        }
    }

    /**
     * 按钮颜色反馈类
     * flag 0-3  对应的是   0：首页   1：报名    2：我的比赛信息   3：退出
     * flag若是true 则设置颜色，反之颜色设为空
     */
    private void setButtonColor() {
        Color color = new Color(228, 230, 234);
        if (flag[0]) {
            index.setBackground(buttonColor);
        } else {
            index.setBackground(color);
        }

        if (flag[1]) {
            baoming.setBackground(buttonColor);
        } else {
            baoming.setBackground(color);
        }

        if (flag[2]) {
            myCompetetion.setBackground(buttonColor);
        } else {
            myCompetetion.setBackground(color);
        }

        if (flag[3]) {
            exit.setBackground(buttonColor);
        } else {
            exit.setBackground(color);
        }

    }



    public static void main(String[] args) {
        SwingUI.setUI();
        JFrame jFrame = new JFrame("11");
        //jFrame.setLayout(null);
        AthleteButtonBar athleteButtonBar = new AthleteButtonBar();
        JPanel buttonBar = athleteButtonBar.getjPanel();
        //buttonBar.setBounds(300,200,500,80);
        jFrame.add(buttonBar);

        jFrame.setSize(800, 600);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

}
